package demo.controller;

import java.util.*;

import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

import demo.controller.entity.*;

@Controller
@RequestMapping("Ajax")
public class Ajax {
	@RequestMapping("GetModelTreeData")
	public ResponseEntity<ModelData[]> getModelTreeData(@RequestParam("MileageID") int mileageID) {
		ModelData parent = null;
		for (ModelData m : getAllModelData()) {
			if (m.getMileageID() == mileageID) {
				parent = m;
				break;
			}
		}
		if (parent instanceof FileModelData) {
			return new ResponseEntity<>(new ModelData[] { parent }, HttpStatus.OK);
		} else if (parent instanceof CompositeModelData) {
			List<ModelData> list = new ArrayList<>();
			for (ModelData m : getAllModelData()) {
				if (m.getParentMileageID() != null && m.getParentMileageID() == mileageID) {
					list.add(m);
				}
			}
			return new ResponseEntity<>(list.toArray(new ModelData[list.size()]), HttpStatus.OK);
		}
		return new ResponseEntity<>(new ModelData[0], HttpStatus.OK);
		//lambda方式
		// ModelData parent = Arrays.stream(getAllModelData()).filter(m ->
		// m.getMileageID() == mileageID).findFirst()
		// .orElseGet(null);
		// if (parent instanceof FileModelData) {
		// return new ResponseEntity<>(new ModelData[] { parent }, HttpStatus.OK);
		// } else if (parent instanceof CompositeModelData) {
		// return new ResponseEntity<>(Arrays.stream(getAllModelData())
		// .filter(m -> m.getParentMileageID() != null && m.getParentMileageID() ==
		// mileageID)
		// .toArray(n -> new ModelData[n]), HttpStatus.OK);
		// }
		// return new ResponseEntity<>(new ModelData[0], HttpStatus.OK);
	}

	@RequestMapping("GetModelData")
	public ResponseEntity<ModelData> getModelData(@RequestParam("MileageID") int mileageID) {
		for (ModelData m : getAllModelData()) {
			if (m.getMileageID() == mileageID) {
				return new ResponseEntity<>(m, HttpStatus.OK);
			}
		}
		return new ResponseEntity<>(null, HttpStatus.OK);
		//lambda方式
		// return new ResponseEntity<>(
		// Arrays.stream(getAllModelData()).filter(m -> m.getMileageID() ==
		// mileageID).findFirst().orElseGet(null),
		// HttpStatus.OK);
	}

	@RequestMapping("GetPropertyTypeItemList")
	public ResponseEntity<JSONObject[]> getPropertyTypeItemList(int MileageID) {
		String specialtyName = "MileageName中心医院";
		String soidModelFile = "HospitalA";
		switch (MileageID) {
		case 1:
			specialtyName = "MileageName房屋";
			soidModelFile = "SampleHouse2";
			break;
		case 2:
			specialtyName = "MileageName楼梯";
			soidModelFile = "ZXYY_CE_A_LT";
			break;
		case 3:
			specialtyName = "MileageName负一层建筑";
			soidModelFile = "ZXYY_CE_Arc_A_B1";
			break;
		case 4:
			specialtyName = "MileageName一层结构";
			soidModelFile = "ZXYY_CE_Str_A_F1";
			break;
		}
		return new ResponseEntity<>(new JSONObject[] { new JSONObject().fluentPut("SOidModelFile", soidModelFile)//
				.fluentPut("MileageID", MileageID)//
				.fluentPut("MileageName", specialtyName) }//
				, HttpStatus.OK);
	}

	@RequestMapping("GetPropertyTypeData")
	public ResponseEntity<JSONObject[]> getPropertyTypeData(int MileageID) {
		return new ResponseEntity<>(new JSONObject[] { new JSONObject().fluentPut("Glid", "") //
				.fluentPut("glid", "glidTypeData") //
				.fluentPut("pGlid", "pGlidTypeData") //
				.fluentPut("name", "nameTypeData") //
				.fluentPut("groupname", "groupnameTypeData") //
				.fluentPut("externalId", "externalIdTypeData") }, HttpStatus.OK);
	}

	@RequestMapping("GetPropertyTypeDataTwo")
	public ResponseEntity<JSONObject[]> getPropertyTypeDataTwo(int MileageID, String pid, String filename) {

		return new ResponseEntity<>(new JSONObject[] { new JSONObject().fluentPut("Glid", "") //
				.fluentPut("glid", "glidTypeDataTwo") //
				.fluentPut("pGlid", "pGlidTypeDataTwo") //
				.fluentPut("name", "nameTypeDataTwo") //
				.fluentPut("groupname", "groupnameTypeDataTwo") //
				.fluentPut("externalId", "externalIdTypeDataTwo") }, HttpStatus.OK);
	}

	@RequestMapping("GetPropertyTreeData")

	public ResponseEntity<JSONObject[]> getPropertyTreeData(int MileageID, String filename) {
		return new ResponseEntity<>(new JSONObject[] { new JSONObject().fluentPut("Glid", "") //
				.fluentPut("glid", "TreeDataglid") //
				.fluentPut("pGlid", "TreeDatapGlid") //
				.fluentPut("name", "TreeDataname") //
				.fluentPut("groupname", "TreeDatagroupname") //
				.fluentPut("externalId", "TreeDataexternalId") }, HttpStatus.OK);
	}

	@RequestMapping("GetPropertyTwo")
	public ResponseEntity<JSONObject[]> getPropertyTwo(int MileageID, String pid, String filename) {
		return new ResponseEntity<>(new JSONObject[] { new JSONObject().fluentPut("Glid", "") //
				.fluentPut("glid", "TreeDataglid2") //
				.fluentPut("pGlid", "TreeDatapGlid2") //
				.fluentPut("name", "TreeDataname2") //
				.fluentPut("groupname", "TreeDatagroupname2") //
				.fluentPut("externalId", "TreeDataexternalId2") }, HttpStatus.OK);
	}

	private ModelData[] getAllModelData() {
		return new ModelData[] { new CompositeModelData(1, null), //
				new FileModelData(2, null, "HospitalA", "中心医院A区"), //
				new FileModelData(3, null, "SampleHouse2", "房子"), //
				new FileModelData(4, 1, "ZXYY_CE_A_LT", "楼梯"), //
				new FileModelData(5, 1, "ZXYY_CE_Arc_A_B1", "负一层建筑"),
				new FileModelData(6, 1, "ZXYY_CE_Str_A_F1", "一层结构"), 
				new FileModelData(9, null, "QK_CE_E_B1_01040927", "新模型B101040927"), 
				new FileModelData(10, null, "QK_CE_E_B2", "新模型B2"), 
				new FileModelData(11, null, "QK_CE_E_B1_01041650", "新模型B1_01041650"), 
				new FileModelData(12, null, "QK_CE_E_B1_Received", "新模型B1_Received"), 
				new FileModelData(13, null, "QK_CE_E_B1_newest", "新模型B1_newest"), 
				new FileModelData(7, null, "QK_CE_E_B1", "新模型B1") };
	}
}
